package com.deadlock8;

public class TestDeadLock3 {
    public static void main(String[] args) {
//        货物
        Object g = new Object();
//        钱
        Object m = new Object();
        Owner s = new Owner(g, m);
        Customer c = new Customer();
        new Thread(s).start();
        new Thread(c).start();



//

    }
}
// 物主
class Owner implements Runnable{
//    货物
    private Object goods;
//    钱
    private Object money;

    public Owner(Object goods,Object money){
        super();
        this.goods = goods;
        this.money = money;
    }

    @Override
    public void run() {
        synchronized (goods){
            System.out.println("先给钱");
            synchronized (money){
                System.out.println("发货");
            }
        }
    }
}
// 顾客
class Customer implements Runnable{
    private Object goods;
    private Object money;
    @Override
    public void run() {
//        可重入锁
        synchronized (money){
            System.out.println("先发货");
            synchronized (goods){
                System.out.println("再给钱");
            }
        }
    }
}
